<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="dataForm.orderNo" placeholder="订单编号"></el-input>
      </el-form-item>
          <el-form-item label="officeNo" prop="officeNo">
          <el-input v-model="dataForm.officeNo" placeholder="officeNo"></el-input>
      </el-form-item>
          <el-form-item label="生单pnr" prop="bookPnr">
          <el-input v-model="dataForm.bookPnr" placeholder="生单pnr"></el-input>
      </el-form-item>
          <el-form-item label="大编码、非必须" prop="bigPnr">
          <el-input v-model="dataForm.bigPnr" placeholder="大编码、非必须"></el-input>
      </el-form-item>
          <el-form-item label="新PNR" prop="pnr">
          <el-input v-model="dataForm.pnr" placeholder="新PNR"></el-input>
      </el-form-item>
          <el-form-item label="支付方式 YPAY-胤钱宝支付 必须" prop="payType">
          <el-input v-model="dataForm.payType" placeholder="支付方式 YPAY-胤钱宝支付 必须"></el-input>
      </el-form-item>
          <el-form-item label="政策ID 必须" prop="dataSourcePolicyNo">
          <el-input v-model="dataForm.dataSourcePolicyNo" placeholder="政策ID 必须"></el-input>
      </el-form-item>
          <el-form-item label="出票类型，已出票订单必填，B2B or BSP 出票 非必须" prop="ticketVoucherType">
          <el-input v-model="dataForm.ticketVoucherType" placeholder="出票类型，已出票订单必填，B2B or BSP 出票 非必须"></el-input>
      </el-form-item>
          <el-form-item label="出票方式: 1-自动出票 2-手动出票" prop="ticketChannels">
          <el-input v-model="dataForm.ticketChannels" placeholder="出票方式: 1-自动出票 2-手动出票"></el-input>
      </el-form-item>
          <el-form-item label="自动出票提示信息" prop="failureReason">
          <el-input v-model="dataForm.failureReason" placeholder="自动出票提示信息"></el-input>
      </el-form-item>
          <el-form-item label="航程类型 1-单程 2-往返 3-多程 必须" prop="flightType">
          <el-input v-model="dataForm.flightType" placeholder="航程类型 1-单程 2-往返 3-多程 必须"></el-input>
      </el-form-item>
          <el-form-item label="是否换编码出票 必须" prop="ifChangePnr">
          <el-input v-model="dataForm.ifChangePnr" placeholder="是否换编码出票 必须"></el-input>
      </el-form-item>
          <el-form-item label="交易流水号 必须" prop="streamNumber">
          <el-input v-model="dataForm.streamNumber" placeholder="交易流水号 必须"></el-input>
      </el-form-item>
          <el-form-item label="联系人 必须" prop="contactName">
          <el-input v-model="dataForm.contactName" placeholder="联系人 必须"></el-input>
      </el-form-item>
          <el-form-item label="联系人电话 必须" prop="contactTel">
          <el-input v-model="dataForm.contactTel" placeholder="联系人电话 必须"></el-input>
      </el-form-item>
          <el-form-item label="联系人邮箱" prop="contactEmail">
          <el-input v-model="dataForm.contactEmail" placeholder="联系人邮箱"></el-input>
      </el-form-item>
          <el-form-item label="联系人备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="联系人备注"></el-input>
      </el-form-item>
          <el-form-item label="订单状态  2出票失败 3支付成功 4正在出票 5已出票 6已取消" prop="orderStatus">
          <el-input v-model="dataForm.orderStatus" placeholder="订单状态  2出票失败 3支付成功 4正在出票 5已出票 6已取消"></el-input>
      </el-form-item>
            <el-form-item label="创建时间" prop="createAte">
          <el-input v-model="dataForm.createAte" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="修改人" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="修改人"></el-input>
      </el-form-item>
          <el-form-item label="修改时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderNo: '',
        officeNo: '',
        bookPnr: '',
        bigPnr: '',
        pnr: '',
        payType: '',
        dataSourcePolicyNo: '',
        ticketVoucherType: '',
        ticketChannels: '',
        failureReason: '',
        flightType: '',
        ifChangePnr: '',
        streamNumber: '',
        contactName: '',
        contactTel: '',
        contactEmail: '',
        remark: '',
        orderStatus: '',
        creator: '',
        createAte: '',
        updator: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        officeNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bookPnr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bigPnr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        pnr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        payType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dataSourcePolicyNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketVoucherType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketChannels: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        failureReason: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        flightType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ifChangePnr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        streamNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contactName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contactTel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contactEmail: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createAte: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/flight/order/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flight/order/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
